package com.yueya.im.node.handler.impl;

import com.yueya.common.util.DateUtils;
import com.yueya.im.common.api.ImContext;
import com.yueya.im.common.util.JsonMapper;
import com.yueya.im.dao.tables.daos.UserGroupDao;
import com.yueya.im.dao.tables.pojos.GroupDO;
import com.yueya.im.dao.tables.pojos.GroupMessageDO;
import com.yueya.im.dao.tables.pojos.UserGroupDO;
import com.yueya.im.node.messages.GroupCreatMessage;
import com.yueya.im.node.messages.GroupTextMsg;
import com.yueya.im.node.messages.MsgType;
import com.yueya.im.service.GroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;

import static com.yueya.im.node.util.StringUtil.byteToString;

@Component
public class GroupMsgHandler extends DefaultHandler {
    @Autowired
    private GroupService groupService;
    @Autowired
    private UserGroupDao userGroupDao;
    @Override
    public void onMessage(ImContext ctx, int code, byte[] message, long msgId) {
        MsgType msgType = MsgType.forNumber(code);
        String text = byteToString(message);
        if(msgType == MsgType.GROUP_TEXT){
            GroupTextMsg groupTextMessage = JsonMapper.getInstance().fromJson(text,GroupTextMsg.class);
            groupTextMessage.setMsgId(msgId);
            groupTextMessage.setMsgType(code);
            groupTextMessage.setDate(System.currentTimeMillis());
            String msg = JsonMapper.toJsonString(groupTextMessage);
            ctx.sendGroupMessage(msgId,msg,code,groupTextMessage.getGroupId());
            ctx.sendToClient(MsgType.GROUP_TEXT_RESP.getNumber(),"");
        }else if(msgType == MsgType.CREATE_GROUP){
            GroupCreatMessage groupCreatMessage = JsonMapper.getInstance().fromJson(byteToString(message), GroupCreatMessage.class);
            GroupDO groupDO = new GroupDO();
            groupDO.setAdminId(Long.valueOf(groupCreatMessage.getAdmin()));
            groupDO.setGroupName(groupCreatMessage.getGroupName());
            groupDO.setGroupIcon(groupCreatMessage.getGroupIcon());
            groupDO = groupService.createGroup(groupDO);
            GroupDO finalGroupDO = groupDO;
            List<UserGroupDO> list = groupCreatMessage.getMemberIds().stream().map(userId -> {
                UserGroupDO userGroupDO = new UserGroupDO();
                userGroupDO.setGroupId(finalGroupDO.getGroupId());
                userGroupDO.setUserId(Long.valueOf(userId));
                userGroupDO.setCreateGmt(DateUtils.getDate());
                userGroupDO.setGroupNickName(finalGroupDO.getGroupName());
                return userGroupDO;
            }).collect(Collectors.toList());
            list.add(new UserGroupDO(finalGroupDO.getGroupId(),finalGroupDO.getAdminId(),groupDO.getGroupName(),DateUtils.getDate()));
            userGroupDao.insert(list);
            ctx.sendToClient(MsgType.CREATE_GROUP_RESP.getNumber(),JsonMapper.toJsonString(groupDO));
        }else if(msgType == MsgType.EXIT_GROUP){
            //todo 退出群组
        }else{
            ctx.sendToClient(MsgType.UNKNOWN_CODE.getNumber(),"");
        }
    }

    @Override
    public void postMessage(ImContext ctx,int msgType, byte[] message,long msgId) {
        // 保存群组聊天历史信息
        if(msgType == MsgType.GROUP_TEXT.getNumber()){
            GroupTextMsg groupTextMessage = JsonMapper.getInstance().fromJson(byteToString(message),GroupTextMsg.class);
            GroupMessageDO msg = new GroupMessageDO();
            msg.setGroupId(Long.valueOf(groupTextMessage.getGroupId()));
            msg.setContent(groupTextMessage.getContent());
            msg.setFromId(Long.valueOf(groupTextMessage.getFrom()));
            msg.setContentType(msgType);
            msg.setMsgId(msgId);
            msg.setFromNickName(groupTextMessage.getNickName());
            msg.setCreateGmt(DateUtils.getCurTimeStamp());
            groupService.saveGroupMsg(msg);
        }
    }
}
